package com.ll.notice.dao.impl;

import com.ll.notice.Utils.PropertiesUtils;
import com.ll.notice.dao.ISmsContentMapper;
import com.ll.notice.entity.SmsNotice;
import org.apache.log4j.Logger;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class SmsContentMapperImpl implements ISmsContentMapper {

    private final static Logger log = Logger.getLogger(SmsContentMapperImpl.class);

    public static final Properties properties = PropertiesUtils.loadProperties("/database.properties");

    static final String JDBC_DRIVER = properties.getProperty("JDBC_DRIVER");

    static final String DB_URL = properties.getProperty("DB_URL");

    static final String USER = properties.getProperty("USER");

    static final String PASSWORD = properties.getProperty("PASSWORD");

    @Override
    public List<SmsNotice> findSmsContentList() {

        Connection conn = null;
        Statement stmt = null;
        List<SmsNotice> smsNotices = new ArrayList<SmsNotice>();
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            log.info("连接数据库...，URL：" + DB_URL + "，账户：" + USER + "，密码："+PASSWORD);
            conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);

            // 执行查询
            log.info(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT\n" +
                    "\tc.sms_id,\n" +
                    "\tc.attending_doctor_id,\n" +
                    "\tc.sms_content,\n" +
                    "\tn.receiver_mobile,\n" +
                    "\tn.sending_status_id\n" +
                    "FROM\n" +
                    "\t`sms_notice` n\n" +
                    "LEFT JOIN sms_content c ON n.sms_id = c.sms_id where n.sending_status_id = 'waiting'";
            log.info(sql);
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                String smsId = rs.getString("sms_id");
                String attendingDoctorId  = rs.getString("attending_doctor_id");
                String smsContent = rs.getString("sms_content");
                String receiverMobile = rs.getString("receiver_mobile");
                String sendingStatusId = rs.getString("sending_status_id");
                SmsNotice smsNotice = new SmsNotice();
                smsNotice.setSmsId(smsId).setAttendingDoctorId(attendingDoctorId).setSmsContent(smsContent).setReceiverMobile(receiverMobile).setSendingStatusId(sendingStatusId);
                smsNotices.add(smsNotice);
            }
            log.info("查询结束");
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) {
                    stmt.close();
                }
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) {
                    conn.close();
                }
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        return smsNotices;
    }

    @Override
    public void update(String sms_id) {

        Connection conn = null;
        Statement stmt = null;
        List<SmsNotice> smsNotices = new ArrayList<SmsNotice>();
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            String sql;
            sql = "UPDATE `sms_notice` SET sending_status_id = 'sent' where sms_id = '"+sms_id+"'";
            log.info(sql);
            stmt = conn.prepareStatement(sql);
            stmt.executeUpdate(sql);
            // 完成后关闭
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) {
                    stmt.close();
                }
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) {
                    conn.close();
                }
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
    }


    public static void main(String args[] ){
        ISmsContentMapper iSmsContentMapper = new SmsContentMapperImpl();
        iSmsContentMapper.update("2");
    }
}
